package com.lcache.common.api;

import com.alibaba.fastjson.JSON;
import lombok.Data;

/**
 * @program: u-social
 * @description: 结果返回对象
 * @author: Alex Wu
 * @createDate: 2025-01-20 17:40
 **/
@Data
public class ApiResponse<T>{
    private long code;
    private String message;

    private T data;

    protected ApiResponse() {
    }

    protected ApiResponse(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> ApiResponse<T> success(T data) {
        return new ApiResponse<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> ApiResponse<T> fail(long code, String message) {
        return new ApiResponse<>(code, message, null);
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
